Skip to main content

pacenotes_installer\engine\execute/
game_files.rs

1/// Through a temp file renamed into place, since a truncated file would be
2/// loaded by the game and then hash as someone else's change.
3pub fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<(), super::ExecuteError> {
4    create_parent(path)?;
5    let temp = path.with_extension("pacenotes-part");
6    let writing = || format!("writing {}", path.display());
7    std::fs::write(&temp, bytes).map_err(|e| super::ExecuteError::io(writing(), e))?;
8    std::fs::rename(&temp, path).map_err(|e| super::ExecuteError::io(writing(), e))
9}
10
11pub fn copy_file(from: &std::path::Path, to: &std::path::Path) -> Result<(), super::ExecuteError> {
12    create_parent(to)?;
13    std::fs::copy(from, to)
14        .map(|_| ())
15        .map_err(|e| super::ExecuteError::io(format!("copying {}", from.display()), e))
16}
17
18pub fn remove_file(path: &std::path::Path) -> Result<(), super::ExecuteError> {
19    std::fs::remove_file(path)
20        .map_err(|e| super::ExecuteError::io(format!("removing {}", path.display()), e))
21}
22
23fn create_parent(path: &std::path::Path) -> Result<(), super::ExecuteError> {
24    let Some(parent) = path.parent() else {
25        return Ok(());
26    };
27    std::fs::create_dir_all(parent)
28        .map_err(|e| super::ExecuteError::io(format!("creating {}", parent.display()), e))
29}